﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using calc;

namespace main
{


    public class MaskCreate : MonoBehaviour
    {
        public static GameObject Canvas = GameObject.Find("Canvas");
        public static GameObject other=null;

        public static GameObject operationWindow=null;
        public static Text resultField=null;
        public static Text num1=null;
        public static Text num2=null;
        public static bool isTwo = true;
        public static string operation;
        public static Text currText;
  

       

        public static void createMask(bool isTwoNum, string op)
        {
            if (!resultField)
            {
                resultField = Canvas.transform.Find("baseWindow/resultArea/Text").gameObject.GetComponent<Text>();
            }
            if (!operationWindow)
            {
                operationWindow = Canvas.transform.Find("baseWindow/operation").gameObject;
            }

            if (!other)
            {
                other = Canvas.transform.Find("baseWindow/other").gameObject;


            }
            other.SetActive(true);
            operationWindow.SetActive(false);
            if (!num2)
            {
                num2 = other.transform.Find("num2/inputField").gameObject.GetComponent<Text>();

            }
            if (!num1)
            {
                num1 = other.transform.Find("num1/inputField").gameObject.GetComponent<Text>();
            }
                num2.transform.parent.gameObject.SetActive(false);

            if (isTwoNum)
            {
                num2.transform.parent.gameObject.SetActive(true);
            }
            isTwo = isTwoNum;
            currText = num1;
            other.GetComponent<otherPro>().operation = op;
        }

        public static void reBackToOperation()
        {
            operationWindow.SetActive(true);
            other.SetActive(false);
        }

        public static void clearNum()
        {
            num1.text = "0";
            resultField.text = "0";
            if (isTwo)
            {
                num2.text = "0";
            }
        }

        public static void backNum()
        {
            if(currText.text.Length > 0)
            {
                string a = currText.text;
                currText.text = a.Substring(0, a.Length - 1);

            }

        }

        public static void compute()
        {
            string oper = other.GetComponent<otherPro>().operation;
            Operation op = OperationFactory.createOperation(oper);
            op.numA = double.Parse(num1.text);
            if (isTwo)
            {
                op.numB = double.Parse(num2.text);
            }

            double result = op.getResult();
            resultField.text = result.ToString();
            
        }

        public static void chooseText(string text)
        {
            Debug.Log("chooseText:" + text);
            if (text.Equals("num1"))
            {
                currText = num1;

            }
            else if(text.Equals("num2"))
            {
                currText = num2;
            }
        }

        public static void addNum(string num)
        {
            Debug.Log("addNum" + num);
            if (currText.text.Equals("0"))
            {
                currText.text = num;
            }
            else
            {
                currText.text += num;

            }
        }


        

    }

}
